﻿#include "replay.h"

Replayer replay;

Replayer::Replayer()
{
	current_frame = 0;
	frame_count = 0;
	keys_pressed = 0;
	frames.reserve(300000);
}

void Replayer::AddFrame(float time)
{
	Replay_Frame r;
	r.time = time;
	r.player_x = player.position.x;
	r.player_y = player.position.y;
	r.player_a = player.focus_angle.to_radians();
	r.player_focus = player.focused ? 1 : 0;
	r.player_shot = player.shooting ? 1 : 0;
	r.player_rapid = player.rapid ? 1 : 0;
	r.player_spellcard_trigger = player.bomb_trigger ? 1 : 0;
	r.player_keys = keys_pressed;

	frames.push_back(r);

	current_frame++;
	frame_count++;
	keys_pressed = 0;
}

void Replayer::SetKeysPressed()
{
	keys_pressed = 1;
}

void Replayer::SaveReplay(CL_String filename)
{
	CL_Directory::create("replay");
	CL_String fname("replay\\");
	fname = fname + filename + CL_String(".rpy");
	CL_File file(fname, CL_File::open_always, CL_File::access_write);
	// Записать информацию об уровне, времени и прочем в заголовок
	file.write_uint32(global_state.version); // Версия игры
	file.write_int8(global_state.current_level); // уровень
	file.write_int8(global_state.lang); // язык
	file.write_int8(global_state.difficulty); // сложность
	file.write_uint64(frames.size()); // количество фреймов
	// А теперь записать все фреймы
	for (unsigned int t=0; t < frames.size(); t++)
	{
		file.write_float(frames[t].time);
		file.write_float(frames[t].player_x);
		file.write_float(frames[t].player_y);
		file.write_float(frames[t].player_a);
		char flags = 0;
		flags |= (char)(1 * frames[t].player_focus);
		flags |= (char)(2 * frames[t].player_shot);
		flags |= (char)(4 * frames[t].player_spellcard_trigger);
		flags |= (char)(8 * frames[t].player_rapid);
		flags |= (char)(16 * frames[t].player_keys);
		file.write_int8(flags);
	}
	file.close();
}

int Replayer::LoadReplay(CL_String filename)
{
	CL_String fname("replay\\");
	fname = fname + filename + CL_String(".rpy");
	if (!CL_FileHelp::file_exists(fname))
		return 0;
	CL_File file(fname, CL_File::open_always, CL_File::access_read);
	// Сначала проверка версий
	unsigned int ver = file.read_uint32();
	if (global_state.version != ver)
	{
		file.close();
		return 0;
	}
	// Шапка
	global_state.current_level = file.read_int8();
	global_state.lang = file.read_int8();
	global_state.difficulty = file.read_int8();
	cl_ubyte64 fsize = file.read_uint64();
	frame_count = fsize;
	// И все фреймы
	frames.clear();
	for (unsigned int t=0; t < fsize; t++)
	{
		Replay_Frame r;
		r.time = file.read_float();
		r.player_x = file.read_float();
		r.player_y = file.read_float();
		r.player_a = file.read_float();
		char flags = file.read_int8();
		r.player_focus = flags & 1 ? 1 : 0;
		r.player_shot = flags & 2 ? 1 : 0;
		r.player_spellcard_trigger = flags & 4 ? 1 : 0;
		r.player_rapid = flags & 8 ? 1 : 0;
		r.player_keys = flags & 16 ? 1 : 0;

		frames.push_back(r);
	}
	file.close();
	current_frame = 0;
	return 1;
}

void Replayer::ClearReplay()
{
	frames.clear();
	keys_pressed = 0;
}

void Replayer::ResetReplay()
{
	current_frame = 0;
}

Replay_Frame Replayer::GetFrameData()
{
	if (frames.size() > 0)
		return frames[current_frame];
	else
	{
		AddFrame(0);
		return frames[0];
	}
}

bool Replayer::NextFrame()
{
	if (current_frame >= frames.size())
		return false;
	else
	{
		current_frame++;
		return true;
	}
}
